<template>
    
    <el-card class="!border-none " shadow="never">
        
        <div class=" flex flex-1 flex-row">
            <el-form class="-mb-4 " :model="queryParams" inline>
                <el-form-item label="岗位编码">
                    <el-input
                        class="w-64"
                        v-model="queryParams.code"
                        clearable
                        @keyup.enter="resetOnePage"
                    >
                    
                    </el-input>
                </el-form-item>
                
                <el-form-item label="岗位名称">
                    <el-input
                        class="w-64"
                        v-model="queryParams.name"
                        clearable
                        @keyup.enter="resetOnePage"
                    />
                </el-form-item>
                
                
                <el-form-item label="岗位状态">
                    <el-select class="w-64" v-model="queryParams.isStop">
                        <el-option label="全部" value/>
                        <el-option label="正常" value="0"/>
                        <el-option label="停用" value="1"/>
                    </el-select>
                </el-form-item>
            </el-form>
            
            
            <div class="flex-1  flex flex-row-reverse">
                <el-button class="mx-2" type="primary" @click="getPageLists">查询</el-button>
                <el-button class="mx-2" @click="resetParams">重置</el-button>
            </div>
        </div>
    </el-card>
    
    
    <el-card class="!border-none mt-4" shadow="never">
        <el-button v-perms="['system:post:add']" type="primary" @click="handleAdd()">
            <template #icon>
                <icon name="el-icon-Plus"/>
            </template>
            新增
        </el-button>
        
        
        <el-table class="mt-4" size="large" v-loading="pagerData.loading" :data="pagerData.lists">
            
            <el-table-column label="岗位编码" prop="code" min-width="100"/>
            <el-table-column label="岗位名称" prop="name" min-width="100"/>
            <el-table-column label="排序" prop="sort" min-width="100"/>
            <el-table-column label="备注" prop="remarks" min-width="100"/>
            
            <el-table-column label="添加时间" prop="createTime" min-width="180"/>
            <el-table-column label="岗位状态" prop="isStop" min-width="100">
                <template #default="{ row }">
                    <el-tag class="ml-2" :type="row.isStop ? 'danger' : ''">
                        {{ row.isStop ? '停用' : '正常' }}
                    </el-tag>
                </template>
            </el-table-column>
            
            <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ row }">
                    <el-button
                        v-perms="['system:post:edit']"
                        type="primary"
                        link
                        @click="handleEdit(row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        v-perms="['system:post:del']"
                        type="danger"
                        link
                        @click="handleDelete(row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        
        
        <div class="flex justify-end mt-4">
            <pagination v-model="pagerData" @change="getPageLists"/>
        </div>
    
    </el-card>
    
    <Edit v-if="showEdit" ref="editRef" @success="getPageLists" @close="showEdit = false"/>

</template>


<script lang='ts' setup>
import Edit from "./edit.vue"
import {usePagerManager} from "@/manager/base/PagerManager";
import {Ref} from "vue";
import feedback from "@/utils/feedback";
import {getPostLists, postDelete} from "@/api/system/SystemAuthPostController";


let editRef = shallowRef(Edit);

const queryParams = reactive({
    code: '',
    name: '',
    isStop: ''
})

const showEdit: Ref<boolean> = ref(false);


/**
 * 分页数据
 */
const {pagerData, getPageLists, resetParams, resetOnePage} = usePagerManager({
    fetchFun: getPostLists,
    params: queryParams
})


/**
 * 新增
 * @param row
 */
async function handleAdd() {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
    
}


/**
 * 编辑
 *
 * @param row
 */
async function handleEdit(row?: Record<string, any>) {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(row)
    
    
}


/**
 * 删除
 *
 * @param row
 */
async function handleDelete(row?:any) {
    
    await feedback.confirm('确定要删除？')
    await postDelete({id : row.id})
    feedback.msgSuccess('删除成功')
    getPageLists()
}



onMounted(() => {
    getPageLists()
})


</script>
